####################################
# 主Makefile不做配置,只进行编译依赖指定 #
####################################
include folder.mk
include pkg/pkg.mk

main:
	@$(GCC) -o out main.cpp $(FE_SRCS) $(FE_INCS) $(FE_LIBS) $(FE_FLAGS) -g && \
	echo "Compile successfully, output $(ROOT_PATH)/out"

clean:
	@rm -rf $(ROOT_PATH)/out*

cleanall: clean
	@rm -rf $(LIBS_PATH)/*

help:
	@echo "Help:" && \
	echo "  make       ## build main application" && \
	echo "  make libs  ## build all libs" && \
	echo "" && \
	echo "Check Platform Build Config:" && \
	echo "  ARCH: $(ARCH)" && \
	echo "  CROSS: $(CROSS)" && \
	echo "  GCC: $(GCC)" && \
	echo "  GXX: $(GXX)" && \
	echo "  AR: $(AR)" && \
	echo "  AS: $(AS)" && \
	echo "  NM: $(NM)" && \
	echo "  RANLIB: $(RANLIB)" && \
	echo "  STRIP: $(STRIP)" && \
	echo "  OBJDUMP: $(OBJDUMP)" && \
	echo "  LD: $(LD)" && \
	echo "  EXT_CFLAGS: $(EXT_CFLAGS)" && \
	echo "  EXT_INCS: $(EXT_INCS)" && \
	echo "  EXT_LIBS: $(EXT_LIBS)" && \
	echo "" && \
	echo "Check FE Build Config:" && \
	echo "  FE_INCS: $(FE_INCS)" && \
	echo "  FE_LIBS: $(FE_LIBS)" && \
	echo "  FE_FLAGS: $(FE_FLAGS)" && \
	echo ""

# ----- 第三方库编译 -----

# 编译所有库
libs: zlib png libtool jpeg iniparser freetype fethirdparty
	@echo "===== make libs success !! ====="

# 打包所有静态库为 fethirdparty.so 用于动态编译部署
fethirdparty:
	$(GCC) -fPIC -shared -Xlinker -zmuldefs -o $(LIBS_PATH)/lib/libfethirdparty.so \
	$(EXT_CFLAGS) \
	-Wl,--whole-archive \
	$(LIBS_PATH)/lib/libfreetype.a \
	$(LIBS_PATH)/lib/libiniparser.a \
	$(LIBS_PATH)/lib/libjpeg.a \
	$(LIBS_PATH)/lib/libpng16.a \
	$(LIBS_PATH)/lib/libz.a \
	-Wl,--no-whole-archive

freetype-sys-gcc=gcc
ifeq ($(GCC),clang)
freetype-sys-gcc=clang
endif
freetype:
	$(freetype-dpkg) $(PKG_PATH)/$(freetype-name).$(freetype-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(freetype-name) && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--host=$(ARCH) \
		--enable-static \
		--with-pic \
		--with-zlib=no \
		--with-harfbuzz=no \
		CC=$(GCC) \
		LD=$(LD) \
		STRIP=$(STRIP) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		CPPFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LIBPNG_CFLAGS="-I$(LIBS_PATH)/include" \
		LIBPNG_LIBS="-L$(LIBS_PATH)/lib" && \
	sed -i 's%gcc%$(freetype-sys-gcc)%' ./builds/unix/unix-cc.mk && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(freetype-name)

iniparser:
	@$(iniparser-dpkg) $(PKG_PATH)/$(iniparser-name).$(iniparser-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(iniparser-name) && \
	make -j4 CC=$(GCC) AR=$(AR) ADDITIONAL_CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC" && \
	mkdir -p $(LIBS_PATH)/lib $(LIBS_PATH)/include && \
	cp libiniparser.a $(LIBS_PATH)/lib/ && \
	cp libiniparser.so* $(LIBS_PATH)/lib/ && \
	cp src/iniparser.h $(LIBS_PATH)/include/ && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(iniparser-name)

jpeg:
	@rm -rf $(LIBS_PATH)/jpeg-* && \
	$(jpeg-dpkg) $(PKG_PATH)/$(jpeg-name).$(jpeg-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/jpeg-6b && \
	sed -i 's%\x0D%%' ./configure && \
	sed -i 's%\x0D%%' ./ltconfig && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--target=$(ARCH) \
		--host=$(ARCH) \
		--enable-static \
		--enable-shared && \
	cp $(LIBS_PATH)/bin/libtool ./ && \
	sed -i 's%CC= gcc%CC= $(GCC)%' ./Makefile && \
	sed -i 's%AR= ar%AR= $(AR)%' ./Makefile && \
	sed -i 's%AR2= ranlib%AR2= $(RANLIB)%' ./Makefile && \
	sed -i 's%CFLAGS=%CFLAGS= $(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC %' ./Makefile && \
	sed -i 's%LDFLAGS=%LDFLAGS= $(EXT_CFLAGS) %' ./Makefile && \
	mkdir -p $(LIBS_PATH)/man/man1 && \
	make -j4 && make install && \
	sed -i '/^#define JPEGLIB_H/a\#include <stddef.h>' ../include/jpeglib.h && \
	cd - && \
	rm -rf $(LIBS_PATH)/jpeg-*

libtool-host=$(ARCH)
ifeq ($(ARCH),aarch64)
libtool-host=arm
endif
libtool:
	@$(libtool-dpkg) $(PKG_PATH)/$(libtool-name).$(libtool-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(libtool-name) && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--host=$(libtool-host) \
		--with-pic \
		--disable-ltdl-install \
		CC=$(GCC) \
		CXX=$(GXX) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		CPPFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(libtool-name)

# 生成.so时 --enable-shared 反则 --enable-shared=no
png:
	$(png-dpkg) $(PKG_PATH)/$(png-name).$(png-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(png-name) && \
	./configure \
		-prefix=$(LIBS_PATH) \
		--host=$(ARCH) \
		--enable-static \
		--enable-shared \
		--with-pic \
		CC=$(GCC) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib -l:libz.a" \
		CPPFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(png-name)

# 生成.so时删去 --static
zlib:
	$(zlib-dpkg) $(PKG_PATH)/$(zlib-name).$(zlib-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(zlib-name) && \
	sed -i '1i\CC=$(GCC)' ./configure && \
	sed -i '1i\CFLAGS=$(EXT_CFLAGS)' ./configure && \
	./configure --prefix=$(LIBS_PATH) && \
	sed -i 's%AR=ar%AR=$(AR)%' ./Makefile && \
	sed -i 's%RANLIB=ranlib%RANLIB=$(RANLIB)%' ./Makefile && \
	sed -i 's%CFLAGS=%CFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC %' ./Makefile && \
	sed -i 's%SFLAGS=%SFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) %' ./Makefile && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(zlib-name)
